<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/1/14-15:59
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\controller;


    use app\admin\model\Logs;
    use app\admin\service\AdminUserService;
    use Jrk\File;
    use think\Exception;
    use think\facade\Cache;

    class Log extends AdminBase
    {


        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub

            $this->model = new Logs();
            $this->del   = "日志记录";
        }

        /**
         * @return mixed
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/14
         * @name: index
         * @describe:获取分页数据
         */
        public function index()
        {
            //获取分页数据
            if (IS_AJAX) {
                return $this->model->getAdminPageData($this->param);
            }

            $user = AdminUserService::userList();
            $this->assign("user", $user);

            return $this->fetch();
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/24
         * @name: logFile
         * @describe:
         */
        public function logFile()
        {
            $file = $this->getDirectory();

            $this->assign("filelist", $file);

            return $this->fetch();
        }



        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/24
         * @name: getContent
         * @describe:
         */
        public function getContent()
        {
            $path = $this->request->post("path");
            try {
                $strlogs = File::read_file($path);
                $this->success("", "", $strlogs);

            } catch (Exception $exception) {
                $this->error($exception->getMessage());
            }
        }


        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/24
         * @name: del_file
         * @describe:
         */
        public function del_file()
        {
            $path = $this->request->post("path");
            $type = $this->request->post("type");

            try {
                if ($type == "file") {
                    @unlink($path);
                    $this->success("删除文件成功");
                } else {
                    $k = File::del_dir($path);
                    if ($k) {
                        $this->success("删除目录成功");
                    } else {
                        $this->error("删除目录失败");
                    }
                }
            } catch (Exception $exception) {
                $this->error($exception->getMessage());
            }


        }

        /**
         * @return bool
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/24
         * @name: getDirectory
         * @describe: 获取目录
         */
        public function getDirectory()
        {
            $directory = $this->getSelectedDirectory();

            return $directory;
        }

        /**
         * @return bool
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/24
         * @name: getSelectedDirectory
         * @describe:获取选中最新日志的文件目录
         */
        public function getSelectedDirectory()
        {
            $directory = $this->directory(RUNTIME_PATH);
            $directory = array_filter(
                $directory,
                function ($item){
                    return !empty($item['children']);
                }
            );
            $directory = array_merge($directory);

            return $this->setSelected($directory);
        }

        /**
         * @param $directory
         * @return bool
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/24
         * @name: setSelected
         * @describe:(简单)自动选中最新日志
         */
        public function setSelected($directory)
        {
            $last = count($directory) - 1;

            if (empty($directory[$last])) {
                return false;
            }

            if ($directory[$last]['type'] == 'folder') {
                if ($directory[$last]['children'] != []) {
                    $directory[$last]['children'] = $this->setSelected($directory[$last]['children']);
                }
            } else {
                // $directory[$last]['state'] = ['selected' => true];
            }

            return $directory;
        }

        /**
         * @param $dir
         * @return array|bool
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/24
         * @name: directory
         * @describe:递归获取目录
         */
        public function directory($dir)
        {
            $dir = rtrim($dir, DIRECTORY_SEPARATOR);
            if (false === is_dir($dir)) {
                return false;
            }
            $fiels     = scandir($dir);
            $directory = [];
            foreach ($fiels as $k => $v) {
                if ($v == '.' || $v == '..') {
                    continue;
                }
                $path = $dir.DIRECTORY_SEPARATOR.$v;
                $id   = str_replace(RUNTIME_PATH, '', $path);
                if (is_dir($path)) {
                    $directory[] = [
                        'id'       => $id,
                        'title'    => $v,
                        'type'     => 'folder',
                        'field'    => '',
                        'path'     => $path,
                        'children' => $this->directory($path),

                    ];
                } else {
                    $directory[] = [
                        'id'    => $id,
                        'title' => $v,
                        'type'  => 'file',
                        'field' => '',
                        'path'  => $path,
                    ];
                }
            }

            return $directory;
        }



    }